import time

from django.http import JsonResponse, HttpResponse
from django.shortcuts import render, redirect
from web.forms.project import ProjectModelForm
from web import models
from utils.tencent.cos import create_bucket

def project_list(request):
    # print(request.tracer.user.username)
    # print(request.tracer.price_policy)
    if request.method == 'GET':
        """
        项目展示部分（思路）：
        1. 从数据库中获取数据
            1.1 我创建的项目：已星标 未星标
            1.2 我参与的项目：已星标 未星标
        2. 提取数据
            2.1 星标项目
            2.2 我创建的项目
            2.3 我参与的项目
        """
        project_dict={'star':[],'my':[],'join':[]}
        my_project_list=models.Project.objects.filter(creator=request.tracer.user)
        for row in my_project_list:
            if row.star:
                project_dict['star'].append({'value':row,'type':'my'})
            else:
                project_dict['my'].append(row)
        join_project_list=models.ProjectUser.objects.filter(user=request.tracer.user)
        for item in join_project_list:
            if item.star:
                project_dict['star'].append({'value':item.project,'type':'join'})
            else:
                project_dict['join'].append(item.project)
        form = ProjectModelForm(request)
        return render(request,'project_list.html',{'form':form,'project_dict':project_dict})


    form=ProjectModelForm(request,data=request.POST)
    if form.is_valid():
        # 为项目创建一个桶(创建桶和区域)
        # 1. 创建桶
        # 命名
        bucket="{}-{}-1381688644".format(request.tracer.user.mobile_phone,str(int(time.time())))
        region="ap-chengdu"
        create_bucket(bucket,region)
        # 2.创建项目
        # 验证通过:页面填写的字段与数据库中表的字段还需要补充
        # 项目名、颜色、描述+creator：从中间键中获取
        form.instance.bucket=bucket
        form.instance.region=region
        form.instance.creator=request.tracer.user
        # 创建项目
        instance=form.save()
        # 3.项目初始化问题类型
        issues_type_object_list=[]
        for item in models.IssuesType.PROJECT_INIT_LIST:
            issues_type_object_list.append(models.IssuesType(project=instance,title=item))
        models.IssuesType.objects.bulk_create(issues_type_object_list)
        return JsonResponse({'status': True})
    return JsonResponse({'status':False,'error':form.errors})


def project_star(request,project_type,project_id):
    if project_type=='my':
        models.Project.objects.filter(id=project_id,creator=request.tracer.user).update(star=True)
        return redirect('web:project_list')
    if project_type=='join':
        models.ProjectUser.objects.filter(project_id=project_id,user=request.tracer.user).update(star=True)
        return redirect('web:project_list')
    return HttpResponse('请求错误')

def project_unstar(request,project_type,project_id):
    if project_type=='my':
        models.Project.objects.filter(id=project_id,creator=request.tracer.user).update(star=False)
        return redirect('web:project_list')
    if project_type=='join':
        models.ProjectUser.objects.filter(project_id=project_id,user=request.tracer.user).update(star=False)
        return redirect('web:project_list')
    return HttpResponse('请求错误')
